<?php

namespace App\Http\Middleware;

use App\Models\Setting\Setting;
use Closure;
use Exception;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class VerifyApplication
{
    /**
     * Handle an incoming request.
     *
     * @param Request                                       $request
     * @param Closure(Request): (Response|RedirectResponse) $next
     *
     * @return Response|RedirectResponse
     * @throws Exception
     */
    public function handle(Request $request, Closure $next)
    {
        $appkey = $request->header('App-Key');
        if (!$appkey) {
            throw new Exception("服务不可用", 503);
        }
        $setting  = new Setting();
        $response = $next($request);
        $response->headers->set('TrainMonth', $setting->TrainMonth());
        $response->headers->set('TrainBookDay', $setting->getTrainBookDay());
        $response->headers->set('OfflineBookServiceFee', $setting->TrainOfflineBookServiceFee());
        $response->headers->set('OfflineChangeServiceFee', $setting->TrainOfflineChangeServiceFee());
        $response->headers->set('TrainSnapHighSpeedServiceFee', $setting->TrainSnapHighSpeedServiceFee());
        $response->headers->set('Station-Version', official_station_version());
        return $response;
    }
}
